package pw.goyd.game.manager;

import pw.goyd.game.constant.setting.GameSetting;
import pw.goyd.game.model.Room;
import pw.goyd.game.model.Seat;
import pw.goyd.game.server.Log;
import pw.goyd.subscribe.PlayerCustomer;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 玩家进出房间管理
 *
 * @author StanWind
 */
public class RoomManager implements GameSetting {
    public static Set<Room> reayRoom = new HashSet<>();
    private static List<Room> rooms = new ArrayList<>();

    /**
     * 根据房间号获取房间对象
     *
     * @param roomID 房间号
     * @return
     */
    public static Room getRoom(int roomID) {
        if (roomID > -1 && roomID < rooms.size()) {
            return rooms.get(roomID);
        } else {
            return null;
        }
    }

    /**
     * 进入房间
     *
     * @return room 房间对象
     */
    public static Room sitPlayer(PlayerCustomer player, int leastScore) {
        synchronized (rooms) {
            //List<Room> rs = rooms;
            //遍历所有房间
            for (Room r : rooms) {
                //TODO 没有做金币下限判断
                synchronized (r) {  //  锁定该房间访问
                    List<Seat> ss = r.getSeats();
                    //遍历所有座位
                    for (int i = 0; i < MAXTABLESEAT; i++) {
                        if (ss.get(i).getPlayer() == null) {
                            //座位无人
                            ss.get(i).setPlayer(player);
                            player.setRoomID(r.getRoomID());
                            //player.psubRoom();    //  不在这订阅的原因是在进房之后需要分开推送一次和房间内玩家不同的消息
                            return r;
                        }
                    }
                }
            }
        }
        //先写固定值 底分5 最低500
        Room temp = newRoom(5, MIN_ROOM_MONEY);
        //坐下第一个座位
        temp.seats.get(0).setPlayer(player);
        player.setRoomID(temp.getRoomID());
        return temp;


    }

    /**
     * 离开房间
     * 没有做空房间回收
     */
    public static void leaveRoom(PlayerCustomer player) {
        if (player == null)
            return;//???????????

        int roomID = player.getRoomID();
        if (roomID < 0)
            return; //  退出可能执行到这 此时不在房间内

        //遍历所有房间
        Room r = rooms.get(roomID);

        //System.out.println(roomID+"<---->"+rooms.size());
        List<Seat> ss = r.getSeats();
        //遍历所有座位
        for (int i = 0; i < MAXTABLESEAT; i++) {
            PlayerCustomer p = ss.get(i).getPlayer();
            if (p != null && p.getAccount_id().equals(player.getAccount_id())) {
                //座位清空 玩家取消订阅 推送玩家退出包 设置玩家房间信息
                ss.get(i).setPlayer(null);
                player.unsubRoom();
                player.sendLeaveRoom();
                player.setRoomID(-1);
                return;
            }
        }

    }


    /**
     * 新建房间
     *
     * @param score 底分
     * @param gold  最低进房金币
     * @return 房间对象
     */
    private static Room newRoom(int score, int gold) {
        Room room = new Room();
        synchronized (rooms) {
            room.setRoomID(rooms.size());
            room.setLeastGold(gold);
            room.setLeastSocre(score);
            room.setCalcGolds(false);
            rooms.add(room);
            room.leaveList = new HashSet<>();
            room.setIsPlayingPlayer(new ArrayList<>());
        }
        Log.getLogger().info("房间---->新开房间[" + room.getRoomID() + "]");
        return room;
    }


}
